layui.use(['form', 'common', 'upload', 'laydate'], function () {
    let $ = layui.$;
    let form = layui.form;
    let common = layui.common;
    let upload = layui.upload;
    let laydate = layui.laydate;
    // 预加载地图
    common.getLatLon('selectMap');
    // 失去焦点是获取定位的经纬度
    $("#address").blur(function () {
        let addres_text = $(this).val();
        common.getLatLon('selectMap', addres_text);
    });

    /**
     * 营业执照有效期
     */
    laydate.render({
        elem: "#business_license_expire"
    });
    /**
     * 餐饮许可证有效期
     */
    laydate.render({
        elem: "#catering_service_license_expire"
    })
    // 表单验证
    form.verify({
        confirmPwd: function (value, item) {
            if (value !== $("input[name=password]").val()){
                return "密码与确认密码不一致";
            }
        },
        nullImgSrc: function (value, item) {
            console.log(item)
            if (value === ''){
                return '请上传对应的照片';
            }
        }
    });
    // 表单的提交
    form.on("submit(addBiz)", function (data) {
        let load = layer.load();
        $.ajax({
            url: '/biz/doAdd',
            type: 'post',
            data: data.field,
            dataType: 'json',
            success: function (res) {
                layer.close(load);
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        location.href = '/biz/';
                    });
                }else {
                    common.tipMessage(res.msg)
                }
            },
            error: function (res) {
                console.log(res)
            },
            before: function () {
                load;
            }
        });

        return false; // 阻止表单提交
    });

    /**
     * 上传身份证背面
     */
    upload.render({
        elem: '#idback' //绑定元素
        , url: '/upload' //上传接口
        , data: {
            _token: function () {
                return $("input[name=_token]").val();
            }
            , type: 'biz/idcard/'
        }
        , done: function(res){
            //上传完毕回调
            if (res.code === 0){
                common.tipMessage(res.msg, 6);
                $("input[name=_token]").val(res.data['_token']);
                $("#idbackImg").append("<img src='"+res.data['src']+"' style='width: 50px;'>");
                $("input[name=idback]").val(res.data['src']);
            }else {
                common.tipMessage(res.msg);
                $("input[name=_token]").val(res.data['_token']);
            }
        }
        , error: function(){
            //请求异常回调
        }
        , accept: 'jpg, jpeg,png'
        , size: 1024*2
        , acceptMime: 'image/*'
        , number: 1
    });

    /**
     * 上传身份证正面
     */
    upload.render({
        elem: '#idfront' //绑定元素
        ,url: '/upload' //上传接口
        , data: {
            _token: function () {
                return $("input[name=_token]").val();
            }
            , type: 'biz/idcard/'
        }
        ,done: function(res){
            //上传完毕回调
            if (res.code === 0){
                common.tipMessage(res.msg, 6);
                $("input[name=_token]").val(res.data['_token']);
                $("#idfrontImg").append("<img src='"+res.data['src']+"' style='width: 50px;'>");
                $("input[name=idfront]").val(res.data['src']);
            }else {
                common.tipMessage(res.msg);
                $("input[name=_token]").val(res.data['_token']);
            }
        }
        ,error: function(){
            //请求异常回调
            common.tipMessage('请检查服务器是否有问题');
        }
        , accept: 'jpg, jpeg,png'
        , size: 1024*2
        , acceptMime: 'image/*'
        , number: 1
    });

    /**
     * 上传营业执照
     */
    upload.render({
        elem: '#businessImage' //绑定元素
        ,url: '/upload' //上传接口
        , data: {
            _token: function () {
                return $("input[name=_token]").val();
            }
            , type: 'biz/business/'
        }
        ,done: function(res){
            //上传完毕回调
            if (res.code === 0){
                common.tipMessage(res.msg, 6);
                $("input[name=_token]").val(res.data['_token']);
                $("#bLImage").append("<img src='"+res.data['src']+"' style='width: 50px;'>");
                $("input[name=business_license_image]").val(res.data['src']);
            }else {
                common.tipMessage(res.msg);
                $("input[name=_token]").val(res.data['_token']);
            }
        }
        ,error: function(){
            //请求异常回调
            common.tipMessage('请检查服务器是否有问题');
        }
        , accept: 'jpg, jpeg,png'
        , size: 1024*2
        , acceptMime: 'image/*'
        , number: 1
    });

    /**
     * 上传餐饮许可证
     */
    upload.render({
        elem: '#cateringImage' //绑定元素
        ,url: '/upload' //上传接口
        , data: {
            _token: function () {
                return $("input[name=_token]").val();
            }
            , type: 'biz/catering/'
        }
        ,done: function(res){
            //上传完毕回调
            if (res.code === 0){
                common.tipMessage(res.msg, 6);
                $("input[name=_token]").val(res.data['_token']);
                $("#cImage").append("<img src='"+res.data['src']+"' style='width: 50px;'>");
                $("input[name=catering_service_license_image]").val(res.data['src']);
            }else {
                common.tipMessage(res.msg);
                $("input[name=_token]").val(res.data['_token']);
            }
        }
        ,error: function(){
            //请求异常回调
            common.tipMessage('请检查服务器是否有问题');
        }
        , accept: 'jpg, jpeg,png'
        , size: 1024*2
        , acceptMime: 'image/*'
        , number: 1
    });


    /**
     * 点击查看大图
     */
    $(".viewImg").click(function () {
        let img_url = $(this).children('img').attr('src');
        layer.open({
            type: 1,
            title: '查看大图',
            area: 'auto',
            shadeClose: true,
            offset: 'auto',
            content: "<img src='"+img_url+"' style='width: 100%'>" //这里content是一个普通的String
        });
    });
})
